<template>
  <div>
    <el-dialog
      :title="title"
      v-model="visable"
      width="65%"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 950px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 95%;padding-left: 5%;text-align: left;"
        >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="练习账号:" prop="studentID">
              <el-select
              :disabled="true"
                filterable
                v-model="dialogFormData.studentID"
                placeholder="请选择练习账号" 
                clearable
              >
                <el-option
                  v-for="item in studentList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                    <span>
                        <template v-if="isBlank(item.name)">{{ item.phone }}</template>
                        <template v-else>{{ item.name }}</template>
                    </span>
                    <span style="float: right;color: #c7c7c7;">{{ item.account }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="持仓日期:" prop="positionDate">
              <el-date-picker
                v-model="dialogFormData.positionDate"
                type="date"
                placeholder="请选择持仓日期"
                size="small"
                value-format="YYYY-MM-DD"
            />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="持仓市值:" prop="availableAmount">
              <el-input-number v-model="dialogFormData.availableAmount" placeholder="请输入持仓市值"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="浮动盈亏:" prop="floatingProfitLoss">
              <el-input-number v-model="dialogFormData.floatingProfitLoss" placeholder="请输入浮动盈亏"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合约代码:" prop="instrumentCode">
              <el-input v-model="dialogFormData.instrumentCode" placeholder="请输入合约代码"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品种合约:" prop="varietyCode">
              <el-input v-model="dialogFormData.varietyCode" placeholder="请输入标的合约"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="期权类型:" prop="optionType">
              <el-select
                  filterable
                  v-model="dialogFormData.optionType"
                  placeholder="请选择期权类型" 
                  clearable
                >
                  <el-option
                    v-for="item in optionTypeList"
                    :key="item.id"
                    :label="item.dicName"
                    :value="item.dicValue"
                  >
                   <span>{{ item.dicName }}</span>
                    <span style="float: right;color: #c7c7c7;">{{ item.dicValue }}</span>
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行价格:" prop="strikePrice">
              <el-input-number v-model="dialogFormData.strikePrice" placeholder="请输入执行价格"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="买持仓:" prop="buyPosition">
              <el-input-number v-model="dialogFormData.buyPosition" placeholder="请输入买持仓"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="买均价:" prop="buyAveragePrice">
              <el-input-number v-model="dialogFormData.buyAveragePrice" placeholder="请输入买均价"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卖持仓:" prop="sellPosition">
              <el-input-number v-model="dialogFormData.sellPosition" placeholder="请输入卖持仓"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卖均价:" prop="sellAveragePrice">
              <el-input-number v-model="dialogFormData.sellAveragePrice" placeholder="请输入卖均价"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="今日结算价:" prop="todaySettlementPrice">
              <el-input-number v-model="dialogFormData.todaySettlementPrice" placeholder="请输入今日结算价"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昨日结算价:" prop="yesterdaySettlementPrice">
              <el-input-number v-model="dialogFormData.yesterdaySettlementPrice" placeholder="请输入昨日结算价"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易编号:" prop="tradeCode">
              <el-input v-model="dialogFormData.tradeCode" placeholder="请输入交易编号"  clearable/>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="交易保证金:" prop="tradeMargin">
              <el-input-number v-model="dialogFormData.tradeMargin" placeholder="请输入交易保证金"  clearable/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 7%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { updateItem, addItem, getItemByID } from '@/api/oss/positionManage.js'
import { getAll } from '@/api/oss/student.js'
import { selectDictionaryByTypeCode } from '@/api/system/dictionary.js'
import { getUserInfo } from '@/utils/localStorage.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        studentID:getUserInfo().studentID,
        positionDate:"",
        availableAmount:"",
        floatingProfitLoss:"",
        instrumentCode:"",
        varietyCode:"",
        optionType:"",
        strikePrice:"",
        buyPosition:"",
        buyAveragePrice:"",
        sellPosition:"",
        sellAveragePrice:"",
        yesterdaySettlementPrice:"",
        todaySettlementPrice:"",
        tradeMargin:"",
        tradeCode:"",
      }
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        account: [
          { required: true, message: '请选择学员信息', trigger: ['blur', 'change'] },
        ]
      },
      // 学员列表
      studentList:[],
      // 期权类型列表
      optionTypeList:[],
    }
  },
  methods: {
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        if (this.isNotBlank(id)) {
          this.title = '修改持仓信息'
          // 获取修改的信息
          getItemByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增持仓信息'
        }
        // 获取所有学员信息
        getAll({}).then(res=>{
          if (res.code == 1) {
            this.studentList = res.data
          }
        })
        selectDictionaryByTypeCode({"dicTypeCode":"optionType"}).then(res=>{
            if (res.code == 1) {
                this.optionTypeList = res.data
            }
        })
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  :deep(.el-drawer__title) {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small),
  :deep(.el-select) {
    width: 100%;
  }
</style>
